<template>
  <div class="app-container">
    <!--查询表单-->
    <!-- inline 表示让表单在一行显示
     -->
    <el-form :inline="true" class="demo-form-inline">  
      <el-form-item>
        <el-select v-model="questionQuery.subject_parent_id" placeholder="一级分类" @change="subjectLevelOneChanged">
          <el-option
                    v-for="subject in subjectOneList"
                    :key="subject.id"
                    :label="subject.title"
                    :value="subject.id"/>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-select v-model="questionQuery.subject_id" placeholder="二级分类">
          <el-option
                    v-for="subject in subjectTwoList"
                    :key="subject.id"
                    :label="subject.title"
                    :value="subject.id"/>
        </el-select>
      </el-form-item>

      <el-form-item label="添加时间">
        <el-date-picker
          v-model="questionQuery.begin"
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="questionQuery.end"
          type="datetime"
          placeholder="选择截止时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>
    <!-- 表格 -->
    <el-table
      :data="list"
      border
      fit
      highlight-current-row>

      <el-table-column
        label="序号"
        width="90"
        align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

        <!--prop内容与取出的list每一条数据的各个对象对应  -->
      <el-table-column prop="title" label="问题名" width="520" />

      <el-table-column prop="gmtCreate" label="添加时间" width="200"/>

      <el-table-column prop="gmtModified" label="上次修改时间" width="200"/>

      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <router-link :to="'/answer/edit/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
          </router-link>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

  <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"
    />

  </div>
</template>
<script>
import answer from '@/api/tax/answer'
import subject from '@/api/tax/subject'

export default {
    // 写核心代码的位置
    // data: {
        //第一种写法
    // },
    data() {
        // 第二种写法
        return {
            // 定义变量和初始值
            list: null,
            page: 1,
            total: 0,
            limit: 5,
            questionQuery: {},
            subjectOneList: [],
            subjectTwoList: []
        }
    },
    created() {
        this.getList()
        this.getOneSubject() // 一级分类
    },
    methods: {
        getList(page = 1) {//page=1表示page默认值是1，如果传入的参数page=2则page为2
            this.page = page
            answer.getQuestionListPage(this.page, this.limit, this.questionQuery)
                .then(response => {  // 请求成功
                    // response 接口返回的数据
                    // console.log(response)
                    this.list = response.data.items
                    this.total = response.data.total
                    console.log(this.list)
                }) 
                .catch(error => {  // 请求失败
                    console.log(error)
                }) 
        },
        resetData() {  //清空的方法
            this.questionQuery = {}
            this.getList()
        },
        removeDataById(id) {
            this.$confirm('此操作将永久删除此问题, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                // 点击确认执行
                answer.deleteQuestionById(id)
                .then(response => {
                    // 删除成功
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    })
                    this.getList();
                })
            })
        },
        // ===================================分类======================================
        // 查询一级分类
        getOneSubject() {
            subject.getSubjectList()
                .then(response => {
                    this.subjectOneList = response.data.list;
                })
        },
        // 根据一级分类ID查询二级分类  点击事件触动的方法会自动封装参数，直接将value传递过来，不需自己手动传参
        subjectLevelOneChanged(value) {
            for (var i = 0; i < this.subjectOneList.length; i++) {
                var oneSubject = this.subjectOneList[i]
                if (value === oneSubject.id) {
                    this.subjectTwoList = oneSubject.children
                    //把二级分类id值清空
                    this.courseInfo.subjectId = ''
                }
            }
        },
    }
}
</script>